﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Game.Model
{
    /// <summary>
    /// 角色对象的容器
    /// </summary>
    public class CharacterContainer
    {
        /// <summary>
        /// 存储数据
        /// </summary>
        private readonly IDictionary<int, Character> _Characters = 
            new Dictionary<int, Character>();

        /// <summary>
        /// 新建一个角色
        /// </summary>
        public Character NewCharacter(short dataId)
        {
            for (int i = 0; i < int.MaxValue; i++)
            {
                if (!_Characters.ContainsKey(i))
                {
                    return _Characters[i] = new Character(i, dataId);
                }
            }
            throw new OverflowException();
        }

        /// <summary>
        /// 删除一个角色
        /// </summary>
        public bool RemoveCharacter(int characterId)
        {
            return _Characters.Remove(characterId);
        }

        /// <summary>
        /// 获取一个角色
        /// </summary>
        public Character GetCharacter(int characterId)
        {
            if (_Characters.TryGetValue(characterId, out var character))
            {
                return character;
            }
            return null;
        }

        /// <summary>
        /// 获取角色 Id 列表
        /// </summary>
        public int[] GetCharacterIds() => _Characters.Keys.ToArray();
    }
}